package com.tarena.practice.manage.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tarena.practice.manage.mapper.CategoriesMapper;
import com.tarena.practice.manage.mapper.TopicMapper;
import com.tarena.practice.manage.pojo.Categories;
import com.tarena.practice.manage.pojo.Topic;
import com.tarena.practice.manage.util.Test;
import com.tarena.practice.manage.util.Topics;
import com.tarena.practice.manage.util.UploadFileUtils;
import com.tarena.practice.manage.vo.PageUtils;
import com.tarena.practice.manage.vo.SysResult;

@Service
public class TopicService {

	@Autowired
	private TopicMapper topicMapper;

	@Autowired
	private CategoriesMapper categoriesMapper;

	@Autowired
	private UploadFileUtils uploadFileUtils;
	
	public void generate() {
		List<Topics> topics = Test.getTopics();
		for (int i = 0; i < topics.size(); i++) {
			Topics topic = new Topics();
			String explain = Test.getAnswer(topics.get(i));

		}
		System.out.println(topics);
		for (Topics topic : topics) {
			Topic topic2 = new Topic();
			topic2.setTopicAnswer(topic.getAnswer());
			topic2.setTopicContent(topic.getContent());
			topic2.setOptionA(topic.getOptionA());
			topic2.setOptionB(topic.getOptionB());
			topic2.setOptionC(topic.getOptionC());
			topic2.setOptionD(topic.getOptionD());
			topic2.setStatus(1);
			topic2.setCreateTime(new Date());
			topic2.setUpdateTime(new Date());
			topic2.setTopicExplain(topic.getExplain());
			topic2.setTopicImg(topic.getImgUrl());
			topicMapper.insert(topic2);
		}
	}

	public List<Topic> findAllTopic() {
		List<Topic> topicList = new ArrayList<>();
		topicList.addAll(topicMapper.findAllTopic());
		
		return topicList;
	}
	
	/**
	 * 分页查询题目
	 * 
	 * @param page
	 * @param order
	 * @param type 
	 * @param rows
	 * @param keyword
	 * @return
	 */
	public PageUtils<Topic> quertTopicPage(Integer page, Integer pageSize, String keyWord, String order, Integer type) {
		// 封装分页对象
		PageUtils<Topic> pageUtils = new PageUtils<>();
		pageUtils.setCurrentPage(page);
		pageUtils.setKeyWord(keyWord);
		pageUtils.setPageSize(pageSize);
		pageUtils.setOrder(order);
		pageUtils.setType(type);
	
		// 分页查询数据
		List<Topic> topics = topicMapper.queryTopicPage(pageUtils);
		for (int i = 0; i < topics.size(); i++) {
			Topic topic =  topics.get(i);
			Categories category = categoriesMapper.selectByPrimaryKey(topic.getCategoryId());
			topic.setCategoryName(getCategoryDetail(category.getCategoryName(), category));
		}
		// 获取数据总量
		int topicCount = topicMapper.queryTopicCount(pageUtils);
		pageUtils.setTotalRow(topicCount);
		pageUtils.setList(topics);
		return pageUtils;
	}

	/**
	 * 递归查询分类的详情
	 * 
	 * @param categoryDetail
	 * @param categories
	 * @return
	 */
	public String getCategoryDetail(String categoryDetail, Categories categories) {
		if (categories.getParentId() == null) {
			String[] categoryDetails = categoryDetail.split(",");
			if (categoryDetails.length > 3) {
				int lastLen = categoryDetails.length - 1;
				categoryDetail = categoryDetails[0] + "|" + categoryDetails[1] + "| ... | " + categoryDetails[lastLen];
			}
			return categoryDetail;
		} else {
			Categories parentCategory = categoriesMapper.selectByPrimaryKey(categories.getParentId());
			categoryDetail = parentCategory.getCategoryName() + " | " + categoryDetail;
			return getCategoryDetail(categoryDetail, parentCategory);
		}
	}

	/**
	 * 根据题目Id删除题目
	 * @param topicId
	 * @return
	 */
	public SysResult deleteTopicById(Integer topicId) {
		try {
			Topic topic = new Topic();
			topic.setTopicId(topicId);
			topic.setStatus(0);
			topicMapper.updateTopicById(topic);
		} catch (Exception e) {
			e.printStackTrace();
			return SysResult.build(201, "删除失败");
		}
		return SysResult.oK();
	}
	/**
	 * 根据ID查询题目
	 * @param topicId
	 * @return
	 */
	public Topic queryTopicById(Integer topicId) {
		if(topicId == null) {
			return null;
		}
		Topic topic = topicMapper.queryTopicById(topicId);
	
		Categories category = categoriesMapper.selectByPrimaryKey(topic.getCategoryId());
		topic.setCategoryName(getCategoryDetail(category.getCategoryName(), category));
		return topic;
	}

	/**
	 * 题目新增
	 * @param topic
	 * @return
	 */
	public SysResult addTopic(Topic topic) {
		try {
			topic.setCreateTime(new Date());
			topic.setUpdateTime(topic.getCreateTime());
			topic.setStatus(1);
			topicMapper.addTopic(topic);
			
		} catch (Exception e) {
			e.printStackTrace();
			return SysResult.build(201, "修改失败");
		}
		return SysResult.oK();
	}

	/**
	 * 题目编辑
	 * @param topic
	 * @return
	 */
	@Transactional
	public SysResult editTopic(Topic topic) {
		try {
			Topic topicBefore = topicMapper.queryTopicById(topic.getTopicId());
			if(!topic.getTopicImg().equals(topicBefore.getTopicImg())) {
				uploadFileUtils.deleteFile(topicBefore.getTopicImg());
			}
			topic.setUpdateTime(new Date());
			topic.setStatus(1);
			topicMapper.updateTopicContent(topic);
			
		} catch (Exception e) {
			e.printStackTrace();
			return SysResult.build(201, "修改失败");
		}
		return SysResult.oK();
	}

}
